<!--
Computer Science Course 531 - Introduction to Software Engineering
Olive Insurance Client Management System (Client Database Access)
Version 1.0 (Base System)
Spring 2011

-- Source Code Details --
Page Tile: carrierDetails.php
Created By: David Gonzalez, Computer Science (Undergraduate)
Documented By: Darrius Serrant, Computer Science (Undergraduate)
Purpose: User interface allowing the system administrator to quickly add new coverage types
         to the relational database for use in the employeeDetails.php and the agencyGroupDetails.php
         form pages.
Status: Completed. Code optimization pending.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <script LANGUAGE="JavaScript" SRC="CalendarPopup.js"></script>
	<script LANGUAGE="JavaScript">
	var cal = new CalendarPopup();
	</script>
        <title>
            Olive Insurance Client Management System - Group Details
        </title>
        <link rel="stylesheet" type="text/css" href="styles/main.css" />
    </head>
    <body>
        <div id ="contentbox">

            <div id="header">
                <img src="images/header.gif" alt="Olive Insurance Client Management System, Version 1.0" />
            </div>
            <div id="separator">
                <img src="images/separator.gif" alt="" />
            </div>
        <?php
        /*
         * PHP Documentation: Handles the insertion, modification, and deletion of carriers
         *                    based on the attributes presented in the POST header (i.e. INSERT for 'save',
         *                    UPDATE for 'update', and DELETE for 'delete').
         */
        session_start();
        require_once('connectvars.php');
        $display = "yes";
        if(isset($_SESSION['user_name'])){
            if(isset($_POST['save'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

            $name = mysqli_real_escape_string($dbc,trim($_POST['name']));

        if(!empty ($name)){
            $query = "INSERT INTO carrier (Name)"."VALUES ('$name')";
            $data = mysqli_query($dbc, $query) or die("Error Saving");

            mysqli_close($dbc);
            echo "Record Saved <br>";

        }
        else{
            echo "All fields need to be entered!<br>";
        }
        }
        if(isset($_POST['delete'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

            $name = mysqli_real_escape_string($dbc,trim($_POST['name']));

        if(!empty ($name)){
            $query = "SELECT * FROM carrier WHERE Name = '$name'";
            $result = mysqli_query($dbc, $query) or die("Error");
            ?><form action ="carrierDetails.php" method="post"><?php
            while($row = mysqli_fetch_array($result)){
                echo '<input type ="radio" value="'.$row['id'].
                '" name ="todelete[]" />';
                echo $row['Name'].'<br/>';
            }
            ?>

        <input type="submit" name="remove" value="Remove"/>
        <input type="submit" name="reload" value="Back"/>
        <div id="footer">
                Copyright &copy; 2011 by Olive Insurance, LLC. All rights reserved.
        </div>
        </form>
        <?php
        /*
         * PHP Documentation: Handles the insertion, modification, and deletion of carriers
         *                    based on the attributes presented in the POST header (i.e. INSERT for 'save',
         *                    UPDATE for 'update', and DELETE for 'delete'). Continuation of what was
         *                   present in the previous block, but with the additon of search handling.
         *
         */
            $display = "no";
        }else{
            mysqli_close($dbc);
            echo "Must have Carrier name";
        }
        }
        if(isset($_POST['remove'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
            foreach($_POST['todelete'] as $deleteid){
                $query = "DELETE FROM carrier WHERE id = '$deleteid'";
                mysqli_query($dbc,$query) or die("ERROR DELETING");
            }
            $display = "yes";
            mysqli_close($dbc);
            echo "Record Deleted";
        }
        if(isset($_POST['find'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
            foreach($_POST['toupload'] as $uploadid){
                $query = "SELECT * FROM carrier WHERE id = '$uploadid'";
                $data = mysqli_query($dbc,$query) or die("ERROR Finding");
                $row = mysqli_fetch_array($data);
            }
            $display = "No";
            echo "Record Uploaded";
            ?>
        <div id="content">

        <div id="userBlock" class="left">
	<span class="user"><?php echo "(".$_SESSION['user_name'].")"?></span>
	<span class="logout"><a href="logOut.php">log out</a></span> <br />
	<span class="userclass">System Administrator</span>
	</div>

	<p id="backHome">
		<a href="mainPage.php">back to main page</a>
		</p>
		<h3 class="leftclear">
		Carrier Details
        	</h3>

            <div id = "managementForm">
            <form action="carrierDetails.php" method="POST">
            <div id="formfields" class="left">
		<fieldset>
		<legend>Data Entry Fields</legend>
		<p class="errormessage">
		</p>
		<div class="field_container">
		<label for ="name">Carrier Name:</label>
                <input type ="text" id="name" name ="name" value ="<?php echo $row['Name']?>"/>
		</div>
                </fieldset>
		</div>
		<div id="formbuttons">
		<fieldset>
		<legend>Management Actions</legend>
                <input type="submit" class="manageButton" name="save" value="Save Carrier" />
		<input type="submit" class="manageButton" name="delete" value="Delete Carrier" />
		<input type="submit" class="manageButton" name="search" value="Search Carrier" />
		</fieldset>
                </div>
                </form>
		</div>

            <div id="footer">
                Copyright &copy; 2011 by Olive Insurance, LLC. All rights reserved.
            </div>

        </div>
        <?php
            mysqli_close($dbc);
        }
        if(isset($_POST['search'])){
            $dbc = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);

            $name = mysqli_real_escape_string($dbc,trim($_POST['name']));

        if(!empty ($name)){
            $query = "SELECT * FROM carrier WHERE Name = '$name'";
            $result = mysqli_query($dbc, $query) or die("Error");
            ?><form action ="carrierDetails.php" method="post"><?php
            while($row = mysqli_fetch_array($result)){
                echo '<input type ="radio" value="'.$row['id'].
                '" name ="toupload[]" />';
                echo $row['Name'].'<br/>';
            }
            ?>

        <input type="submit" name="find" value="Find"/>
        <input type="submit" name="reload" value="Back"/>
        <div id="footer">
                Copyright &copy; 2011 by Olive Insurance, LLC. All rights reserved.
            </div>
        </form>
        <?php
            $display = "no";
        }else{
            mysqli_close($dbc);
            echo "Must have Carrier name";
        }
        }
        if(isset($_POST['reload'])){
            header('Location: ./carrierDetails.php');
        }
        if($display == "yes"){?>
        <div id="content">

        <div id="userBlock" class="left">
	<span class="user"><?php echo "(".$_SESSION['user_name'].")"?></span>
	<span class="logout"><a href="logOut.php">log out</a></span> <br />
	<span class="userclass">System Administrator</span>
	</div>

	<p id="backHome">
		<a href="mainPage.php">back to main page</a>
		</p>
		<h3 class="leftclear">
		Carrier Details
        	</h3>

            <div id = "managementForm">
            <form action="carrierDetails.php" method="POST">
            <div id="formfields" class="left">
		<fieldset>
		<legend>Data Entry Fields</legend>
		<p class="errormessage">
		</p>
		<div class="field_container">
		<label for ="name">Carrier Name:</label>
                <input type ="text" id="name" name ="name"/>
		</div>
                </fieldset>
		</div>
		<div id="formbuttons">
		<fieldset>
		<legend>Management Actions</legend>
                <input type="submit" class="manageButton" name="save" value="Save Carrier" />
		<input type="submit" class="manageButton" name="delete" value="Delete Carrier" />
		<input type="submit" class="manageButton" name="search" value="Search Carrier" />
		</fieldset>
                </div>
                </form>
		</div>

            <div id="footer">
                Copyright &copy; 2011 by Olive Insurance, LLC. All rights reserved.
            </div>

        </div>
        <?php
                /*
         * PHP Documentation: Login page redirection for unauthenticated users.
         *
         */
        }
        }else{
            ?>
            <div id="loginbox">
                <p class="sectionheader">
                    Authentication Required
                </p>
                <div id="loginform">
                    <div class="invalid">
                        <img src="images/Progress_Wheel.gif" alt="" />This page is restricted from unauthorized users. Please log in.
                    </div>
                </div>
            </div>
             <?php
                header('Refresh: 2; ./loginPage.php');
        }
        ?>
        </div>
    </body>
</html>